package com.ylc.springdemo.spring4.spring4aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;

/**
 * controller的切面
 * 不使用LogAn的原因是该方法打印参数时会有问题，因为该切面是切Controller，有HttpServletRequest参数就不行
 * @author yuanlichao
 */
@Service
@Aspect
public class ControllerAspect {

    @Around("@annotation(com.ylc.springdemo.spring4.spring4aop.aspect.ControllerAnnotation)")
    public Object controllerMethodAround(ProceedingJoinPoint joinPoint)throws Throwable{
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("进入切面"+ className+","+ methodName);
        try {
            return joinPoint.proceed();
        }catch (Throwable throwable){
            System.out.println(joinPoint.getSignature()+"捕获异常");
            throw  throwable;
        }finally {
            System.out.println("离开切面");
        }
    }
}
